//
//  CommentImageCell.swift
//  BaseSwift
//
//  Created by gh on 2019/10/6.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit

class CommentImageCell: UITableViewCell {

    let containerView = UIView()
    let icon = UIImageView()
    let nickLab = UILabel()
    let timeLab = UILabel()
    let contLab = UILabel()
    let typeLab = UILabel()
    let nameLab = UILabel()
    let imgBg = UIView()
    let line = UIView()


    
    func setModel(mod:TopPic) {
        
        contLab.updateLayout()
        line.updateLayout()

        imgBg.removeFromSuperview()

        
        if mod.userPostingImgList.count > 0 {
            
            containerView.addSubview(imgBg)
            var temp = Array<UIView>()
            
            var count = 3
            if mod.userPostingImgList.count < 3 {
                
                count = mod.userPostingImgList.count
            }
            for _ in 0 ..< count {
                
                let imgv = UIImageView()
                imgv.backgroundColor = UIColor.cccColor
                imgBg.addSubview(imgv)
                let _ = imgv.sd_layout()?.heightIs(80)
                let imgvo = mod.userPostingImgList.get(at: count)
                
                imgv.sd_setImage(with: URL(string:imgvo?.imgUrl ?? ""), placeholderImage: UIImage(named: "holder"), options: .lowPriority) { (A, B, C, D) in
                    
                }

                temp.append(imgv)
                
            }
            let _ = imgBg.sd_layout()?.topSpaceToView(contLab,16)?.leftSpaceToView(icon,16)!.rightSpaceToView(containerView,16)
            
            imgBg.setupAutoWidthFlowItems(temp, withPerRowItemsCount: 3, verticalMargin: 0, horizontalMargin: 5, verticalEdgeInset: 0, horizontalEdgeInset: 0)
            
//            line.backgroundColor = UIColor.lineColor
//            containerView.addSubview(line)
//            let _ = line.sd_layout()?.widthRatioToView(containerView,1)!.heightIs(1)!.topSpaceToView(imgBg,16)
        }else{
            
            
//            line.backgroundColor = UIColor.lineColor
//            containerView.addSubview(line)
//            let _ = line.sd_layout()?.widthRatioToView(containerView,1)!.heightIs(1)!.topSpaceToView(contLab,16)
        }
        
        nickLab.text = mod.userName
        timeLab.text = mod.createTime
        icon.sd_setImage(with: URL(string:mod.userHead ?? ""), placeholderImage: UIImage(named: "默认头像"), options: .lowPriority) { (A, B, C, D) in
            
        }
        contLab.text = mod.content
        
    }
    

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.contentView.addSubview(containerView)
        self.containerView.sd_cornerRadius = 8
        self.backgroundColor = UIColor.backgroundColor
        containerView.backgroundColor = UIColor.white
        self.selectionStyle = .none

        let _ = containerView.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: 10, left: 25, bottom: 0, right: 25))
        containerView.sd_addSubviews([icon,nickLab,timeLab,contLab])
        
        icon.backgroundColor = UIColor.cccColor
        let _ = icon.sd_layout()?.widthIs(32)!.heightIs(32)!.leftSpaceToView(containerView,16)!.topSpaceToView(containerView,16)
        icon.sd_cornerRadius = 16
        

        nickLab.textColor = UIColor.textTitleColorBlack
        nickLab.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight(rawValue: 0.2))
        nickLab.text = "核桃酥"
        let _ = nickLab.sd_layout()?.leftSpaceToView(icon,16)!.heightIs(20)!.topSpaceToView(containerView,16)!.rightSpaceToView(containerView,45)
        
        timeLab.textColor = UIColor.textPlaceHolderColor
        timeLab.font = UIFont.smallerFont
        timeLab.text = "2019-09-09"
        let _ = timeLab.sd_layout()?.leftSpaceToView(icon,16)!.heightIs(15)!.topSpaceToView(nickLab,2)!.rightSpaceToView(containerView,45)
        
        contLab.textColor = UIColor.tabbarDefaultTextColor
        contLab.font = UIFont.commonFont
        contLab.text = "这里是我发表的评论，可以删除，照过可以通过左滑查看更多；评论内容超过三行的话，就折叠起来…"
        let _ = contLab.sd_layout()?.leftSpaceToView(icon,16)!.autoHeightRatio(0)!.topSpaceToView(timeLab,16)!.rightSpaceToView(containerView,16)
        contLab.setMaxNumberOfLinesToShow(3)
        
        //图片
        
        containerView.addSubview(imgBg)
        var temp = Array<UIView>()
        
        for _ in 0 ..< 3 {
            
            let imgv = UIImageView()
            imgv.backgroundColor = UIColor.cccColor
            imgBg.addSubview(imgv)
            let _ = imgv.sd_layout()?.heightIs(80)

            temp.append(imgv)
            
        }
        let _ = imgBg.sd_layout()?.topSpaceToView(contLab,16)?.leftSpaceToView(icon,16)!.rightSpaceToView(containerView,16)
        
        imgBg.setupAutoWidthFlowItems(temp, withPerRowItemsCount: 3, verticalMargin: 0, horizontalMargin: 5, verticalEdgeInset: 0, horizontalEdgeInset: 0)
        
        let line = UIView()
        line.backgroundColor = UIColor.lineColor
        containerView.addSubview(line)
        let _ = line.sd_layout()?.widthRatioToView(containerView,1)!.heightIs(1)!.topSpaceToView(imgBg,16)
        line.isHidden = true
        
        let bottomView = UIView()
        bottomView.isHidden = true
        containerView.addSubview(bottomView)
        let _ = bottomView.sd_layout()?.widthRatioToView(containerView,1)!.topSpaceToView(line,0)!.bottomSpaceToView(containerView,0)
        bottomView.addSubview(typeLab)
        typeLab.textColor = UIColor.GP_Blue_Color
        typeLab.textAlignment = .center
        typeLab.text = "景点"
        typeLab.font = UIFont.commonFontSmaller
        let _ = typeLab.sd_layout()?.leftSpaceToView(bottomView,16)!.centerYEqualToView(bottomView)!.widthIs(35)!.heightIs(16)
        typeLab.sd_cornerRadius = 2
        typeLab.layer.borderColor = UIColor.GP_Blue_Color.cgColor
        typeLab.layer.borderWidth = 1
        
        nameLab.font = UIFont.commonFont
        nameLab.textColor = UIColor.textTitleColorBlack
        nameLab.text = "断桥残雪"
        bottomView.addSubview(nameLab)
        let _ = nameLab.sd_layout()?.leftSpaceToView(typeLab,16)!.heightIs(20)!.centerYEqualToView(bottomView)!.rightSpaceToView(bottomView,35)
        
        let rigIcon = UIImageView()
        rigIcon.image = UIImage(named: "向右")
        bottomView.addSubview(rigIcon)
        let _ = rigIcon.sd_layout()?.widthIs(16)!.heightIs(16)!.centerYEqualToView(bottomView)!.rightSpaceToView(bottomView,16)
        
        
        
        

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
